﻿using System;
using System.Xml.Linq;
using Microsoft.Xna.Framework.Content.Pipeline;
using Phoenix.Xna.Framework.ContentPipeline.Properties;
using Phoenix.Xna.Framework.Xml;

namespace Phoenix.Xna.Framework.ContentPipeline.Models
{
    internal struct SplitTask
    {
        public string ParentAnimation;
        public int AnimationFramerate;
        public string ID;
        public float StartTime;
        public float EndTime;

        public SplitTask(string id, float startTime, float endTime)
        {
            ID = id;
            StartTime = startTime;
            EndTime = endTime;
            ParentAnimation = string.Empty;
            AnimationFramerate = 0;
        }

        private void ProcessStartTime(string startTimeValue, string startFrameValue, SplitTaskParameters parameters)
        {
            if (!string.IsNullOrEmpty(startTimeValue))
            {
                try
                {
                    StartTime = float.Parse(startTimeValue);
                }
                catch (Exception)
                {
                    throw new InvalidContentException(string.Format(Resources.InvalidStartTimeException,
                        ID,
                        parameters.AnimationID));
                }
            }
            else if (!string.IsNullOrEmpty(startFrameValue))
            {
                try
                {
                    int startFrame = int.Parse(startFrameValue);
                    StartTime = startFrame / parameters.Framerate;
                }
                catch (Exception)
                {
                    throw new InvalidContentException(string.Format(Resources.InvalidStartTimeException,
                        ID,
                        parameters.AnimationID));
                }
            }
            else
            {
                throw new InvalidContentException(string.Format(Resources.InvalidStartTimeException,
                    ID,
                    parameters.AnimationID));
            }
        }

        private void ProcessEndTime(string endTimeValue, string endFrameValue, SplitTaskParameters parameters)
        {
            if (!string.IsNullOrEmpty(endTimeValue))
            {
                try
                {
                    EndTime = float.Parse(endTimeValue);
                }
                catch (Exception)
                {
                    throw new InvalidContentException(string.Format(Resources.InvalidEndTimeException,
                        ID,
                        parameters.AnimationID));
                }
            }
            else if (!string.IsNullOrEmpty(endFrameValue))
            {
                try
                {
                    int endFrame = int.Parse(endFrameValue);
                    EndTime = endFrame / parameters.Framerate;
                }
                catch (Exception)
                {
                    throw new InvalidContentException(string.Format(Resources.InvalidEndTimeException,
                        ID,
                        parameters.AnimationID));
                }
            }
            else
            {
                throw new InvalidContentException(string.Format(Resources.InvalidEndTimeException,
                    ID,
                    parameters.AnimationID));
            }
        }

        private void Validate(SplitTaskParameters parameters)
        {
            if (EndTime <= StartTime)
            {
                throw new InvalidContentException(string.Format(Resources.AnimationEndTimeException,
                    ID,
                    parameters.AnimationID));
            }

            if (StartTime < 0)
            {
                throw new InvalidContentException(string.Format(Resources.AnimationPositiveStartTimeException,
                    ID,
                    parameters.AnimationID));
            }

            if (EndTime > parameters.Duration.TotalSeconds)
            {
                throw new InvalidContentException(string.Format(Resources.AnimationEndTimeOverflowException,
                    ID,
                    parameters.AnimationID));
            }
        }

        #region Object Members

        public override string ToString()
        {
            return string.Format("{0} [{1} : {2}]", ID, StartTime, EndTime);
        }

        #endregion

        #region IXmlSerializable Members

        public void LoadXElement(XElement element)
        {

        }

        public void LoadXElement<T>(XElement element, T parameters)
        {
            object o = parameters;
            SplitTaskParameters p = (SplitTaskParameters)o;
            ID = element.ReadAttribute<string>("ID");
            string startTimeValue = element.ReadAttribute<string>("StartTime");
            string endTimeValue = element.ReadAttribute<string>("EndTime");
            string startFrameValue = element.ReadAttribute<string>("StartFrame");
            string endFrameValue = element.ReadAttribute<string>("EndFrame");

            if (string.IsNullOrEmpty(ID))
            {
                throw new InvalidContentException(string.Format(Resources.AnimationTagException, p.AnimationID));
            }

            ProcessStartTime(startTimeValue, startFrameValue, p);
            ProcessEndTime(endTimeValue, endFrameValue, p);
            Validate(p);
        }

        public System.Xml.Linq.XElement ToXElement()
        {
            throw new System.NotImplementedException();
        }

        #endregion
    }
}
